﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PublicOfficialSelectionService;
using System.Collections.ObjectModel;
using CommunitySystem.KaoHeService;
using CommunitySystem.EmpbasicService;
using EHR.Common;
using System.IO;
using System.Windows.Data;

namespace CommunitySystem.PageView
{
    public partial class SYQMKHControl : UserControl
    {

        private PublicOfficialSelectionServiceClient downLoadClient = new PublicOfficialSelectionServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<EmpbasicService.OrgForHRModel> OrgList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();
        private KaoHeServiceClient client1 = new KaoHeServiceClient();
        public SYQMKHControl()
        {
            InitializeComponent();
            ListBoxOfLife.Items.Clear();
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            Client.GetDengLuOrgCompleted+=new EventHandler<GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
        
            client1.GetSYQMKHCompleted += new EventHandler<CommunitySystem.KaoHeService.GetSYQMKHCompletedEventArgs>(client1_GetSYQMKHCompleted);
            client1.GetSYQMKHCountCompleted += new EventHandler<CommunitySystem.KaoHeService.GetSYQMKHCountCompletedEventArgs>(client1_GetSYQMKHCountCompleted);

            List<string> khjg = new List<string>() { "  ","合格", "不合格", "顺延试用期" };
            this.cbKHJG.ItemsSource = khjg;
            this.cbKHJG.SelectedIndex = 0;
            this.cbKHJG.SelectedIndex = 0;

            List<string> khzt = new List<string>() { "  ", "已上报", "审核通过", "已退回" };
            this.cbKHZT.ItemsSource = khzt;
            if(Session.LoginUser.RoleID==1)  //管理员角色默认看到已上报
                this.cbKHZT.SelectedIndex = 1;
            else //人事干部默认看到 未上报 和已退回的
            this.cbKHZT.SelectedIndex = 0;
        
        }
        private int pageSize = 20;
        void client1_GetSYQMKHCountCompleted(object sender, CommunitySystem.KaoHeService.GetSYQMKHCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
        }

        void client1_GetSYQMKHCompleted(object sender, CommunitySystem.KaoHeService.GetSYQMKHCompletedEventArgs e)
        {
            _Attachment.Clear();
            DGRole.ItemsSource = null;
            if (e.Result != null && e.Result.Count > 0)
            {
                for (int i = 0; i < e.Result.Count; i++)
                {
                   // e.Result[i].Sequence = i + 1;

                    foreach (var item in e.Result[i].Attachment)
                    {
                        if (!_Attachment.Contains(_Attachment.Where(p => p.realname == item.realname && p.MainID == item.MainID).FirstOrDefault()))
                            _Attachment.Add(item);
                    }

                }
                DGRole.ItemsSource = e.Result;
            }
        }

        #region 获得组织机构树
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }

        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;
                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        public int orgid { get; set; }
        public List<string> orgids = new List<string>();
        public string type { get; set; }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                //if (orgids != null)
                //{
                //orgid = Convert.ToInt32(((EmpbasicService.OrgForHRModel)cb.DataContext).ID);
                //tbTitle.Text = cb.Content.ToString();
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
              
                CheckChildItem(cb.Tag as TreeViewItem);

                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
            }
            else
            {
                //if (orgids != null)
                //{
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
            
                UnCheckChildItem(cb.Tag as TreeViewItem);

                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (ss.Length > 1) ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
            }
        }

        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        #endregion
        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            orgid = Convert.ToInt32(((OrgForHRModel)(ListBoxOfLife.SelectedItem)).ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

     
        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            ListBoxOfLife.ItemsSource = e.Result;
            ListBoxOfLife.SelectedIndex = 0;
            orgid = Convert.ToInt32(e.Result[0].ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            if (e.Result != "人事干部")
            {
                if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Collapsed;

                this.ibtnTG.Visibility = Visibility.Visible;
                this.ibtnTH.Visibility = Visibility.Visible;
                Client.GetOrgForHRModelAsync(0);
                OId = "";
                LoadData("");
            }
            else
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Visible;

                this.ibtnTG.Visibility = Visibility.Collapsed;
                this.ibtnTH.Visibility = Visibility.Collapsed;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
            }
        }
        public String[] Oldorgids;
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                Oldorgids = e.Result.Split(',');
            }
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }
       /// <summary>
        /// 上报
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            //var seljw = this.DGRole.SelectedItem as Emp_SYQMKHModel;
            if (this.DGRole.ItemsSource != null)
            {
                foreach (Emp_SYQMKHModel i in (ObservableCollection<Emp_SYQMKHModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                if (selectedempmodel.Count > 1)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能选择一个人员进行考核上报！");
                    return;
                }
               var seljw =selectedempmodel.FirstOrDefault();
               if (string.IsNullOrEmpty(seljw.Status) || seljw.Status == "已退回")
               {
                   if (seljw != null)
                   {
                       SYQMKHWindow win = new SYQMKHWindow(seljw);
                       win.Show();
                       win.Closed += new EventHandler(win_Closed);

                   }
               }
               else SilverlightMessageBoxLibrary.Message.ErrorMessage("该人员考核上报已审核通过,不能进行考核上报！");

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要上报的考核人员！");
        }

        void win_Closed(object sender, EventArgs e)
        {
            LoadData(OId);
        }
        /// <summary>
        /// 通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TG_Click(object sender, RoutedEventArgs e)
        {
            if (this.DGRole.ItemsSource != null)
            {
                foreach (Emp_SYQMKHModel i in (ObservableCollection<Emp_SYQMKHModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (var item in selectedempmodel)
                {
                    if (string.IsNullOrEmpty(item.Status))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核未上报,不能进行审核！");
                        return;
                    }
                    if (item.Status == "已退回")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核上报已退回,不能进行审核！");
                        return;
                    }
                    if (item.Status == "审核通过")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核上报已审核通过,不能进行审核！");
                        return;
                    }
                }
                if (MessageBox.Show("确定要审核吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string Ids = string.Empty;
                    foreach (var item in selectedempmodel)
                    {
                        Ids += item.ID + ",";
                    }
                    SH_SYQMKHWindow win = new SH_SYQMKHWindow(Ids.TrimEnd(','), "审核通过");
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要审核的考核上报人员！");

        }
        /// <summary>
        /// 退回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TH_Click(object sender, RoutedEventArgs e)
        {

            if (this.DGRole.ItemsSource != null)
            {
                foreach (Emp_SYQMKHModel i in (ObservableCollection<Emp_SYQMKHModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (var item in selectedempmodel)
                {
                    if (string.IsNullOrEmpty(item.Status))
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核未上报,不能进行退回！");
                        return;
                    }
                    if (item.Status == "已退回")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核上报已退回,不能进行退回！");
                        return;
                    }
                    if (item.Status == "审核通过")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage(item.Name + " 考核上报已审核通过,不能进行退回！");
                        return;
                    }
                }
                if (MessageBox.Show("确定要退回吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string Ids = string.Empty;
                    foreach (var item in selectedempmodel)
                    {
                        Ids += item.ID + ",";
                    }
                    SH_SYQMKHWindow win = new SH_SYQMKHWindow(Ids.TrimEnd(','), "已退回");
                    win.Show();
                    win.Closed += new EventHandler(win_Closed);
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要退回的考核上报人员！");
        }
        private void ibtnQuery_Click(object sender, RoutedEventArgs e)
        {
            LoadData(OId);
        }
        private string OId { get; set; }
        public void LoadData(string orgids)
        {
            OId = orgids;
            selectedempmodel.Clear();
            string tj = QueryCon(orgids);
            client1.GetSYQMKHCountAsync(tj);
        }

        private string QueryCon(string orgids)
        {
            string tj = string.Empty;
            if (!string.IsNullOrEmpty(orgids))
                tj = string.Format(" and OrgID in ({0}) ", orgids);
            if (!string.IsNullOrEmpty(this.cbKHJG.SelectedItem.ToString()) && this.cbKHJG.SelectedItem.ToString() != "  ")
                tj += string.Format(" and KHStatus='{0}' ", this.cbKHJG.SelectedItem.ToString());
            if (!string.IsNullOrEmpty(this.cbKHZT.SelectedItem.ToString()) && this.cbKHZT.SelectedItem.ToString() != "  ")
                tj += string.Format(" and Status='{0}' ", this.cbKHZT.SelectedItem.ToString());

            //查询条件都为空
            if (!string.IsNullOrEmpty(this.cbKHJG.SelectedItem.ToString()) && this.cbKHJG.SelectedItem.ToString() == "  " && !string.IsNullOrEmpty(this.cbKHZT.SelectedItem.ToString()) && this.cbKHZT.SelectedItem.ToString() == "  ")
            {
                //管理员
                if (Session.LoginUser.RoleID == 1)
                    tj += string.Format(" and (Status is null or Status='') ");
                else
                    tj += string.Format(" and (Status is null or Status='' or Status='已退回')");
            }
            return tj;
        }

        private ObservableCollection<Emp_SYQMKHFJModel> _Attachment =
            new ObservableCollection<Emp_SYQMKHFJModel>();
     
        private void Init(int id)
        {
            if (dgwp!=null)
            dgwp.Children.Clear();

            HyperlinkButton hb = null;
            foreach (var item in _Attachment.Where(p => p.MainID == id).ToList())
            {
                hb = new HyperlinkButton() { Margin = new Thickness(10, 0, 0, 0), VerticalAlignment = VerticalAlignment.Center };
                hb.Tag = item;
                hb.Content = item.realname;
                hb.Click += new RoutedEventHandler(hb_Click);
                dgwp.Children.Add(hb);
            }
        }
        private List<byte> fileContent = new List<byte>();
        void hb_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hbtn = sender as HyperlinkButton;

            var attach = hbtn.Tag as Emp_SYQMKHFJModel;

            SilverlightMessageBoxLibrary.CustomMessage cm = new SilverlightMessageBoxLibrary.CustomMessage("确认要下载此方案吗?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
            cm.Show();
            cm.OKButton.Click += (sender1, e1) =>
            {

                string extend = attach.realname.Substring(attach.realname.LastIndexOf('.'));
                var openFile = new SaveFileDialog() { Filter = string.Format("{0}|*.{0}", extend) };
                if (openFile.ShowDialog() == true)
                {
                    fileContent.Clear();
                    downLoadClient.DownLoadFileCompleted -= new EventHandler<PublicOfficialSelectionService.DownLoadFileCompletedEventArgs>(downLoadClient_DownLoadFileCompleted);
                    downLoadClient.DownLoadFileCompleted += new EventHandler<PublicOfficialSelectionService.DownLoadFileCompletedEventArgs>(downLoadClient_DownLoadFileCompleted);

                    downLoadClient.DownLoadFileAsync(new PublicOfficialSelectionService.DownLoadFile() { PathKey = "file", HasFinished = false, Lenght = 1600, FileName = attach.filename, StartPosition = 0 }, openFile.OpenFile());
                }

            };
        }

        void downLoadClient_DownLoadFileCompleted(object sender, PublicOfficialSelectionService.DownLoadFileCompletedEventArgs e)
        {
            var down = e.Result;
            fileContent.AddRange(down.Content);
            if (down.HasFinished == true)
            {
                var stream = e.UserState as Stream;
                stream.Write(fileContent.ToArray(), 0, fileContent.Count);
                stream.Dispose();
                stream.Close();
                SilverlightMessageBoxLibrary.Message.InfoMessage("下载完毕!");
            }
            else
            {
                down.StartPosition += down.Lenght;
                downLoadClient.DownLoadFileAsync(down, e.UserState);
            }
        }

      

        void RMWindow_Closed(object sender, EventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        private WrapPanel dgwp = null;
        private void DGRole_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            Emp_SYQMKHModel bindData = (Emp_SYQMKHModel)e.Row.DataContext;
            dgwp = DGRole.Columns[10].GetCellContent(e.Row).FindName("wp") as WrapPanel;

            Init(bindData.ID);
        }

        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;

            string tj = QueryCon(OId);
            client1.GetSYQMKHAsync(tj, index + 1, pageSize);
        }
        /// <summary>
        /// 多选
        /// </summary>
        /// <param name="sender"></param>ii
        /// <param name="e"></param>
        public ObservableCollection<Emp_SYQMKHModel> selectedempmodel =
         new ObservableCollection<Emp_SYQMKHModel>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (Emp_SYQMKHModel p in this.DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }
        }

    }
}
